package com.project.nimsen.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.UseDto;
import com.project.nimsen.entity.Site;
import com.project.nimsen.entity.User;
import com.project.nimsen.service.ISiteService;
import com.project.nimsen.service.IUserService;
import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
import com.project.jdbc.utils.MD5Encrypt;
@Repository("userService")
public class UserServiceImpl extends BaseServiceImpl implements IUserService {
	@Resource
	IBaseDao baseDao;
	@Resource
	ISiteService siteService;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public User findUserByName(String userName) throws Exception {
		Finder finder = new Finder("select id,username,password,mail,regtime,active"
			+ " from user where active!=1 and username=:username");
		finder.setParam("username", userName);
		return baseDao.queryForObject(finder, User.class);
	}
	public List<Map<String, Object>> findAjaxUser(String userName)
			throws Exception {
		Finder finder = new Finder("select id,username from user where active!=1");
		if(StringUtils.isNotBlank(userName)){
			finder.append(" and username=:username");
			finder.setParam("username", "%"+userName+"%");
		}
		return baseDao.queryForList(finder);
	}
	public List<Map<String, Object>> find(UseDto dto) throws Exception {
		Finder finder = new Finder("select us.id,us.username,us.password,us.mail,us.regtime"
			+ ",us.active,si.sitename from user as us left join site as si on us.id=si."
			+ "managerid where us.active!=1");
		finder.append(" order by us.regtime desc");
		return baseDao.queryForList(finder, dto.getPage());
	}
	public boolean saveByDto(UseDto dto) throws Exception {
		//当前时间
		Date nowDate = new Date();
		//保存用户表的信息
		User use = new User();
		use.setActive(0);
		use.setPassword(MD5Encrypt.encoderByMd5With32Bit(dto.getPassword()));
		use.setRegtime(nowDate);
		use.setUsername(dto.getUsername());
		use.setMail(dto.getMail());
		Object useid = baseDao.saveEntity(use);
		if(useid == null)
			return false;
		//保存站点信息
		String struseid = useid.toString();
		Site site = new Site();
		site.setActive(0);
		site.setCreateTime(nowDate);
		site.setCreateUser(dto.getCurrentuseid());
		site.setManagerid(struseid);
		site.setSitename(dto.getSitename());
		return baseDao.saveEntity(site)!=null;
	}
	public User findUserById(String useid) throws Exception {
		if(StringUtils.isBlank(useid))
			return null;
		Finder finder = new Finder("select id,username,password,mail,regtime,active"
				+ " from user where active!=1 and id=:id");
		finder.setParam("id", useid);
		return baseDao.queryForObject(finder, User.class);
	}
	public String deleteById(String useid) throws Exception {
		if(StringUtils.isBlank(useid))
			return "没有此用户！";
		if(!siteService.deleteByManagerId(useid))
			return "删除站点失败！";
		delete(useid);
		return null;
	}
	/**
	 * 删除一个用户表信息
	 * */
	private boolean delete(String useid)throws Exception{
		Finder finder = new Finder("update user set active=1 where active!=1 and id=:id");
		finder.setParam("id", useid);
		return baseDao.update(finder)>0;
	}
}
